package cn.haohaowo.webservice.servlet;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletConfig;

import org.apache.cxf.endpoint.Server;
import org.apache.cxf.frontend.ServerFactoryBean;
import org.apache.cxf.service.invoker.BeanInvoker;
import org.apache.cxf.transport.servlet.CXFNonSpringServlet;
import org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor;
import org.apache.ws.security.WSConstants;
import org.apache.ws.security.handler.WSHandlerConstants;

import cn.haohaowo.webservice.HelloWorldService;
import cn.haohaowo.webservice.HelloWorldServiceImpl;
import cn.haohaowo.webservice.server.ServerPasswordHandler;

public class WebServiceServlet extends CXFNonSpringServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6620489173017030655L;
	
	@Override
	protected void loadBus(ServletConfig sc) {
		super.loadBus(sc);
//		System.out.println("###################################");
//		HelloWorldService helloWorldService = new HelloWorldServiceImpl();
//		Endpoint.publish("/helloWorldService", helloWorldService);
		
		System.out.println("############################");
		ServerFactoryBean serverFactoryBean = new ServerFactoryBean();
		serverFactoryBean.setServiceClass(HelloWorldService.class);
		serverFactoryBean.setAddress("/helloWorldService");
		HelloWorldService helloWorldService = new HelloWorldServiceImpl();
		serverFactoryBean.setInvoker(new BeanInvoker(helloWorldService));
		Server server = serverFactoryBean.create();
		org.apache.cxf.endpoint.Endpoint endpoint = server.getEndpoint();
		Map<String, Object> inProps = new HashMap<String, Object>();
		inProps.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);
		inProps.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_DIGEST);
		inProps.put(WSHandlerConstants.PW_CALLBACK_CLASS, ServerPasswordHandler.class.getName());
		endpoint.getInInterceptors().add(new WSS4JInInterceptor(inProps));
	}

}
